<?php
	check_login();
	$a = getAction();

	switch($a) {
		case 'index':
			$sql = " select *  from " . table('order') . " where status <> '-1' order by id desc";
			$url="admin.php?m=order";

			$rscount=$db->getOne("select count(1) from ($sql) temp");
			$pagesize=20;
			$page=max(1,intval($_GET['page']));
			$start=($page-1)*$pagesize;
			$sql.=" limit $start,$pagesize";
			
			$list = $db->getAll($sql);
			
			for ($i = 0; $i < count($list); $i++) {
				$list[$i]["user"] = $db->getRow("select * from " . table("user") . " where openid = '" .$list[$i]["openid"] . "'");
				$list[$i]["status_name"] = ORDER_STATUS::getOrderStatusName($list[$i]["status"]);
				
				if ($list[$i]["status"] == ORDER_STATUS::FAIL) {
					$list[$i]["fail_reason"] = MAI_STATUS_CODE::$STATUS_CODE_ARRAY[$list[$i]["mai_status_code"]];
				}
				
				$list[$i]["total_fee"] = $list[$i]["total_fee"] / 100.0;
				
				if($list[$i]["promoter_id"]) {
					$list[$i]["promoter_name"] = $db->getOne("select name from " . table("promoter") . " where id = '" .$list[$i]["promoter_id"] . "'");
				}
			}

			$smarty->assign("pagelist",multipage($rscount,$pagesize,$page,$url));
			$smarty->assign("list", $list);

			$smarty->display("order.html");
			break;	
		case 'bonus':
			$sql = " select *  from " . table('give_order') . " order by id desc";
			$url="admin.php?m=order&a=bonus";

			$rscount=$db->getOne("select count(1) from ($sql) temp");
			$pagesize=20;
			$page=max(1,intval($_GET['page']));
			$start=($page-1)*$pagesize;
			$sql.=" limit $start,$pagesize";
			
			$list = $db->getAll($sql);
			
			for ($i = 0; $i < count($list); $i++) {
				$list[$i]["user"] = $db->getRow("select * from " . table("user") . " where openid = '" .$list[$i]["openid"] . "'");
			}

			$smarty->assign("pagelist",multipage($rscount,$pagesize,$page,$url));
			$smarty->assign("list", $list);

			$smarty->display("order_bonus.html");
			break;	
			
		case 'bonus_edit':
			$id = htmlspecialchars($_GET["id"]);
			
			if ($_POST) {
				$id = $_POST["id"];
				$give_value = $_POST["give_value"];
				
				$db->query("update " . table("give_order") . " set give_value = '$give_value' where id = '$id'");
				gourl();
				exit();
			}
			
			$sql = " select *  from " . table('give_order') . " where id = '$id'";
			
			$formData = $db->getRow($sql);
			$formData["user"] = $db->getRow("select * from " . table("user") . " where openid = '" .$formData["openid"] . "'");
				
			$smarty->assign("formData", $formData);
			
			$smarty->display("order_bonus_edit.html");
				
}
?>